<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
        * {
            padding: 0;
            margin: 0;
            list-style: none;
            border: 0;
        }

        .all {
            width: 500px;
            height: 200px;
            padding: 7px;
            border: 1px solid #ccc;
            margin: 100px auto;
            position: relative;
        }

        .screen {
            width: 500px;
            height: 200px;
            overflow: hidden;
            position: relative;
        }

        .screen li {
            width: 500px;
            height: 200px;
            overflow: hidden;
            float: left;
        }

        .screen ul {
            position: absolute;
            left: 0;
            top: 0px;
            width: 3000px;
        }

        .all ol {
            position: absolute;
            right: 10px;
            bottom: 10px;
            line-height: 20px;
            text-align: center;
        }

        .all ol li {
            float: left;
            width: 20px;
            height: 20px;
            background: #fff;
            border: 1px solid #ccc;
            margin-left: 10px;
            cursor: pointer;
        }

        .all ol li.current {
            background: yellow;
        }

        #arr {
            display: none;
            z-index: 1000;

        }

        #arr span {
            width: 40px;
            height: 40px;
            position: absolute;
            left: 5px;
            top: 50%;
            margin-top: -20px;
            background: #000;
            cursor: pointer;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
            font-family: '黑体';
            font-size: 30px;
            color: #fff;
            opacity: 0.3;
            border: 1px solid #fff;
        }

        #arr #right {
            right: 5px;
            left: auto;
        }
    </style>
</head>
<body>
	<div class="all" id='box'>
    	<div class="screen">
        	<ul>
            	<li><img src="./images/wf1.jpg" width="500" height="200"/></li>
            	<li><img src="./images/wf2.jpg" width="500" height="200"/></li>
            	<li><img src="./images/wf3.jpg" width="500" height="200"/></li>
            	<li><img src="./images/wf4.jpg" width="500" height="200"/></li>
            	<li><img src="./images/wf5.jpg" width="500" height="200"/></li>
        	</ul>
        	<ol>
        	</ol>
    	</div>
    	<div id="arr"><span id="left">&lt;</span><span id="right">&gt;</span></div>
	</div>

	<script src="../js/common.js"></script>
	<script src="../js/animate.js"></script>
	<script>
		var box = my$('box');
		var screen = box.children[0];
		var ul = screen.children[0];
		var ol = screen.children[1];
		var arr = my$('arr');
		var arrLeft = my$('left');
		var arrRight = my$('right');
		// 图片的宽度
		var imgWidth = screen.offsetWidth;
		// 1.动态生成序列
		var count = ul.children.length;
		for (var i = 0; i < count; i++) {
			var li = document.createElement('li');
			ol.appendChild(li);
			setInnerText(li, i + 1);
			// 让第一个li高亮显示
			if (i === 0) {
				ol.children[0].className = 'current';
			}
			// 点击li让当前li高亮显示 取消其他li的高亮显示
			li.onclick = liClick;
			// 2.点击序列图片移动
			var liIndex = i;
			index = liIndex;
			li.setAttribute('index', liIndex);
		}
		function liClick () {
			for (var i = 0; i < ol.children.length; i++) {
				var li = ol.children[i];
				li.className = '';
			}
			this.className = 'current';
			var index = parseInt(this.getAttribute('index'));
			animate(ul, -index * imgWidth);
		}
		
		// 3.生成左右按钮
		box.onmouseover = function () {
			arr.style.display = 'block';
			clearInterval(timeId);
		}
		box.onmouseout = function () {
			arr.style.display = 'none';
			timeId = setInterval(function() {
				arrRight.click();
			}, 1000)
		}
		// 4.点击左右按钮时图片移动
		var index = 0;
		arrRight.onclick = function () {
			if (index === count) {
				index = 0;
				ul.style.left = 0 + 'px';
			}
			index++;
			if (index < count) {
				ol.children[index].click();
			} else {
				animate(ul, -index * imgWidth);
				for (var i = 0; i < ol.children.length; i++) {
					var li = ol.children[i];
					li.className = '';
				}
				ol.children[0].className = 'current';
			}
		}
		arrLeft.onclick = function () {
			if (index === 0) {
				index = count;
				ul.style.left = -index * imgWidth + 'px';
			}
			index--;
			ol.children[index].click();
		}
		// 5.图片自动播放
		var timeId = setInterval(function () {
			arrRight.click();
		}, 1000);

		// 将ul里的第一个li克隆放在最后
		var cloneLi = ul.children[0].cloneNode(true);
		ul.appendChild(cloneLi);
	</script>
</body>
</html>